package com.dingding.manager.modules.dirver.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dingding.facade.user.dto.common.driver.ReqDriverAuthDTO;
import com.dingding.manager.common.dao.Page;
import com.dingding.manager.common.exception.BussinessException;
import com.dingding.manager.common.exception.enums.BizExceptionEnum;
import com.dingding.manager.core.util.ToolUtil;
import com.dingding.manager.modules.dirver.dao.DriverManagerDao;
import com.dingding.manager.modules.dirver.dto.ReqAddDriverDTO;
import com.dingding.manager.modules.dirver.dto.RespDetailDriverDTO;
import com.dingding.manager.modules.dirver.dto.RespGetDriverDTO;
import com.dingding.manager.modules.dirver.dto.RespResult;
import com.dingding.manager.modules.dirver.service.DriverManagerService;

/**
 * 
 * @Description: 司机管理业务相关Controller
 * @author haoxiaojun
 * @date 2016年12月3日 下午3:18:14
 * @since V2.5.7
 */
@Controller
@RequestMapping("/driver/v1")
public class DriverManagerController {

	Logger log = Logger.getLogger(this.getClass());

	@Resource(name = "driverManagerService")
	private DriverManagerService driverManagerService;
	
	@Resource(name = "driverManagerDao")
	private DriverManagerDao driverManagerDao;

	/**
	 * 
	 * @Description: 司机管理业务之添加司机
	 * @author haoxiaojun
	 * @return String
	 * @throws Exception
	 * @throws IOException
	 * @date 2016年12月3日 下午3:24:10
	 * @since V2.5.7
	 */

	@RequestMapping(value = "/dvadd", method = { RequestMethod.POST })
	@ResponseBody
	public String addDriver(ReqDriverAuthDTO reqDriverAuth, String telPhone, HttpServletRequest request,
			HttpServletResponse response) {

		Boolean flag = driverManagerService.addDriver(reqDriverAuth, telPhone);

		if (flag) {
			return "success";
		} else {
			return "error";
		}
	}

	/**
	 * 
	 * @Description: 司机管理业务之司机资料编辑
	 * @author haoxiaojun
	 * @return String
	 * @date 2016年12月3日 下午3:24:10
	 * @since V 2.5.7
	 */
	@RequestMapping(value = "/dvedit", method = { RequestMethod.POST })
	@ResponseBody
	public String editDriver(ReqAddDriverDTO reqAddDriver, HttpServletRequest request, HttpServletResponse response) {

		Boolean flag = null;
		flag = driverManagerService.editDriver(reqAddDriver);

		if (flag) {
			return "success";
		} else {
			return "error";
		}

	}

	/**
	 * 
	 * @Description: 司机管理业务之修改司机状态
	 * @author haoxiaojun
	 * @return String
	 * @date 2016年12月3日 下午3:24:10
	 * @since V2.5.7
	 */
	@RequestMapping(value = "/dvmodify", method = { RequestMethod.POST })
	public String modifyDriver(String suId, Integer isdel, HttpServletRequest request, HttpServletResponse response) {

		Boolean flag = null;
		flag = driverManagerService.modifyDriver(suId, isdel);

		if (flag) {
			return "success";
		} else {
			return "error";
		}

	}

	/**
	 * 
	 * @Description: 司机管理业务之列表展示司机
	 * @author haoxiaojun
	 * @return RespResult
	 * @date 2016年12月3日 下午3:24:10
	 * @since V2.5.7
	 * @mender ligang 原因：增加司机姓名、手机号、车牌号查询条件
	 */
	@RequestMapping(value = "/dvget", method = { RequestMethod.POST })
	@ResponseBody
	public RespResult<RespGetDriverDTO> getDriver(Integer start, Integer length, Integer cityId, Integer provice_id,
			Integer companyId, Integer suvStatus,String dvName,String submitTime, HttpServletRequest request, HttpServletResponse response) {

		Page<RespGetDriverDTO> driverPage = driverManagerService.getDriver(start, length, cityId, provice_id, companyId,
				suvStatus, dvName,submitTime);

		// datatable固定相应格式
		RespResult<RespGetDriverDTO> results = new RespResult<>();

		results.setAaData(driverPage.getList());
		results.setiTotalRecords(driverPage.getTotalPage());
		results.setiTotalDisplayRecords(driverPage.getTotalRow());

		return results;

	}

	/**
	 * 
	 * @Description: 司机管理业务之司机详情
	 * @author haoxiaojun
	 * @return RespDetailDriverDTO
	 * @date 2016年12月3日 下午3:24:10
	 * @since V2.5.7
	 */
	@RequestMapping(value = "/dvdetail", method = { RequestMethod.POST })
	@ResponseBody
	public RespDetailDriverDTO detailDriver(String suid, HttpServletRequest request, HttpServletResponse response) {

		RespDetailDriverDTO respdetaildriverdto = new RespDetailDriverDTO();
		respdetaildriverdto = driverManagerService.detailDriver(suid);
		return respdetaildriverdto;

	}

	/**
	 * 
	 * @Description: 图片上传
	 * @author haoxiaojun
	 * @return String
	 * @date 2016年12月3日 下午3:24:10
	 * @since V 2.5.7
	 */
	@RequestMapping(value = "/upimage", method = { RequestMethod.POST })
	@ResponseBody
	public String editDriver(String srcImage, HttpServletRequest request, HttpServletResponse response) {
		
		if (ToolUtil.isEmpty(srcImage)) {
			return null;
		}
		String image = null;

		try {
			image = driverManagerService.upLoadImage(srcImage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return image;
	}
	
	/**
	 * @Description 改变司机的登录状态
	 * @param userId 用户id
	 * @param status 要改变的状态
	 * @author fengshuonan
	 * @date 2017年2月7日 下午4:20:17
	 */
	@RequestMapping(value = "/setDriverLoginStatus", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String setDriverLoginStatus(@RequestParam(name="userId") String userId,@RequestParam("status") Integer status) {
		
		if (ToolUtil.isEmpty(userId)) {
			throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
		}
		
		driverManagerDao.setDriverLoginStatus(userId, status);
		
		return "SUCCESS";
	}

}
